package com.labofjet.web;

import java.util.HashMap;
import java.util.Map;

import org.apache.commons.lang3.builder.ToStringBuilder;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.labofjet.exception.BaseException;

/**
 * 用于前后台数据交互时候格式的统一
 * 
 * @author jyzjyz12@163.com
 * 
 */
public class ContextDTO {
    private String stateCode;
    private Map<String, Object> data;
    private BaseException exception;
   
    public BaseException getException() {
        return exception;
    }

    public void setException(BaseException exception) {
        this.exception = exception;
    }

    public String getStateCode() {
        return stateCode;
    }

    public void setStateCode(String stateCode) {
        this.stateCode = stateCode;
    }

    public Map<String, Object> getData() {
        return data;
    }

    public void setData(Map<String, Object> data) {
        this.data = data;
    }

    public <T> T getDTOFromData(String name, Class<T> dtoClass){
        JSONObject o = (JSONObject) data.get(name);
        return JSON.parseObject(o.toJSONString(), dtoClass);
    }
    
    public void putData(String name, Object obj){
        if(data == null){
            data = new HashMap<String, Object>();
        }
        data.put(name, obj);
    }   
    
    @Override
    public String toString() {
        // TODO Auto-generated method stub
        return ToStringBuilder.reflectionToString(this);
    }
}
